﻿Imports System.Web
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Services
Imports System.Web.Script.Services
Imports System.Web.Services.Protocols
Imports VSModel
Imports System.Data.Objects
Imports Cls_Common
Imports System.IO

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class wsAutoComplete
	Inherits System.Web.Services.WebService

	<WebMethod()> _
   <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
	Public Function GetAutoCompleteVendor(ByVal strSrc As String) As List(Of App_Code.ObjectJSON)
		Dim json As New List(Of App_Code.ObjectJSON)
		'ConvertVietNamToNoneVietNam(strSrc)
		Using data As New VSEntities
			'Dim p = data.uspGetPhanLoaiNhomThuocChildSelectAll(strSrc, 0, 10).ToList()
			Dim p = (From q In data.ItemVendors
			  Where q.MaterialCode.Contains(strSrc) Or q.FiscalPeriodPostng.Contains(strSrc) Or strSrc = ""
			   Group By _material = q.MaterialCode Into GroupMaterial = Group
			  Select New With {_material, ._fiscal = GroupMaterial.FirstOrDefault().FiscalPeriodPostng}).ToList()

			''Export du lieu sang kieu du lieu json
			If Not p Is Nothing AndAlso p.Count > 0 Then
				For i As Integer = 0 To p.Count - 1
					Dim objTacGia As New App_Code.ObjectJSON
					objTacGia.ID = p(i)._material
					objTacGia.FullText = p(i)._fiscal
					json.Add(objTacGia)
				Next
			End If
			Return json
		End Using
	End Function
	
	<WebMethod()> _
   <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
	Public Function GetListVendor(ByVal itemCode As String) As List(Of App_Code.ObjectJSON)
		Dim json As New List(Of App_Code.ObjectJSON)
		'ConvertVietNamToNoneVietNam(strSrc)
		Using data As New VSEntities
			Dim p = (From q In data.ItemVendors
			  Where q.MaterialCode.Contains(itemCode)
			  Select q.VendorCode, q.Supplier).ToList()
			''Export du lieu sang kieu du lieu json
			If Not p Is Nothing AndAlso p.Count > 0 Then
				For i As Integer = 0 To p.Count - 1
					Dim objTacGia As New App_Code.ObjectJSON
					objTacGia.ID = p(i).VendorCode
					objTacGia.FullText = p(i).Supplier
					json.Add(objTacGia)
				Next
			End If
			Return json
		End Using
	End Function

	<WebMethod()> _
  <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
	Public Function GetOtherList(ByVal strSrc As String) As List(Of App_Code.ObjectJSON)
		Dim json As New List(Of App_Code.ObjectJSON)

		Using data As New VSEntities

			Dim p = (From q In data.OtherLists
			  Where q.Type.Contains(strSrc)
			  Order By q.PhanLoai
			  Select New With {._Id = q.Id, ._Name = q.Name}).ToList()

			''Export du lieu sang kieu du lieu json
			If Not p Is Nothing AndAlso p.Count > 0 Then
				For i As Integer = 0 To p.Count - 1
					Dim objOtherList As New App_Code.ObjectJSON
					objOtherList.ID = p(i)._Id
					objOtherList.FullText = p(i)._Name
					json.Add(objOtherList)
				Next
			End If
			Return json
		End Using
	End Function

	<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
	Public Function GetOtherListDetail(ByVal strIdOtherListTable As String, ByVal strIdOtherList As String) As String
		Dim strBuilder As New StringBuilder
		'ConvertVietNamToNoneVietNam(strSrc)
		Using data As New VSEntities
			'Dim p = data.uspGetPhanLoaiNhomThuocChildSelectAll(strSrc, 0, 10).ToList()
			Dim p = (From q In data.OtherLists
			  Where q.Type.Contains(strIdOtherListTable) And q.Id = strIdOtherList
			  Order By q.PhanLoai
			  Select New With {q.PhanLoai, q.TuCanhBao, q.CanhBaoNguyCo}).FirstOrDefault

			''Export du lieu sang kieu du lieu json
			If Not p Is Nothing Then
				strBuilder.Append("<div><ul style='padding-left: 20px' ><li>Phân loại: " + p.PhanLoai + "</li><li> Từ cảnh báo: " + p.TuCanhBao + "</li><li>Nguy cơ: " + p.CanhBaoNguyCo + "</li></ul></div>")
			End If
			Return strBuilder.ToString()
		End Using
	End Function

	<WebMethod(EnableSession:=True)> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
	Public Function GetOtherListTableByType(ByVal strType As Integer) As List(Of App_Code.ObjectJSON)
		Dim LanguageId = Session("LanguageId").ToString()
		Dim json As New List(Of App_Code.ObjectJSON)
		Using data As New VSEntities
			Dim p = (From h In data.OtherListTables Where h.LanguageId = LanguageId And
					 h.TypeId = strType Select h.OLCode, h.OtherListName).ToList
			''Export du lieu sang kieu du lieu json
			If Not p Is Nothing AndAlso p.Count > 0 Then
				For i As Integer = 0 To p.Count - 1
					Dim objOtherList As New App_Code.ObjectJSON
					objOtherList.ID = p(i).OLCode
					objOtherList.FullText = p(i).OtherListName
					json.Add(objOtherList)
				Next
			End If
			Return json
		End Using
	End Function

	<WebMethod(EnableSession:=True)> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
	Public Function GetOtherListSection03Detail(ByVal strSection03ID As Integer) As List(Of App_Code.ObjectJSON)
		Dim LanguageId = Session("LanguageId").ToString()
		Dim json As New List(Of App_Code.ObjectJSON)
		Using data As New VSEntities
			'Dim p = (From h In data.OtherListTables Where h.LanguageId = LanguageId And
			'		 h.TypeId =  Select h.OLCode, h.OtherListName).ToList
			Dim p = (From s In data.Section03Detail Join ot In data.OtherListTables On s.TypeOtherListTable Equals ot.OLCode
													Join o In data.OtherLists On o.Id Equals s.IdOtherList And o.Type Equals s.TypeOtherListTable
					Where s.Section03Id = strSection03ID
					Order By ot.TypeId
					Select s, ot.TypeId
					).ToList
			''Export du lieu sang kieu du lieu json
			If Not p Is Nothing AndAlso p.Count > 0 Then
				For i As Integer = 0 To p.Count - 1
					Dim objOtherList As New App_Code.ObjectJSON
					objOtherList.ID = p(i).TypeId
					objOtherList.FullText = p(i).s.TypeOtherListTable & "#" & p(i).s.IdOtherList
					json.Add(objOtherList)
				Next
			End If
			Return json
		End Using
	End Function

	<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
	Public Function getItemBatchNo(ByVal _batchNo As String) As List(Of App_Code.ObjectJSON)
		Dim json As New List(Of App_Code.ObjectJSON)
		'ConvertVietNamToNoneVietNam(strSrc)

		Using data As New VSEntities
			Dim p = (From q In data.WareHouses
				Join h In data.ItemVendors On q.ItemCode Equals h.MaterialCode And q.VendorCode Equals h.VendorCode
		Where q.BatchNo = _batchNo
			  Select q.BatchNo, q.ItemCode, h.FiscalPeriodPostng, q.VendorCode, h.Supplier).FirstOrDefault
			''Export du lieu sang kieu du lieu json
			If Not p Is Nothing Then

				Dim objTacGia As New App_Code.ObjectJSON
				objTacGia.ID = p.BatchNo
				objTacGia.FullText = p.ItemCode & "@@" & p.FiscalPeriodPostng & "@@" & p.VendorCode & "@@" & p.Supplier
				json.Add(objTacGia)
			End If
			Return json
		End Using

	End Function

	<WebMethod()> _
 <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
 Public Function DeleteRelativeFile(ByVal absolutePath As String) As Boolean
		Try
			absolutePath = absolutePath.Replace("@", "/")
			If File.Exists(absolutePath) Then
				File.Delete(absolutePath)
			End If
			Return True
		Catch
			Return False
		End Try
	End Function

	<WebMethod(EnableSession:=True)> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
	Public Function RenderHTMLSection03Detail(ByVal strSection03ID As Integer) As List(Of App_Code.ObjectJSON)
		Dim sbTL As New StringBuilder()
		sbTL.Append("<tr id='{0}' >")
		sbTL.Append("<td width='10%' class='text_list_links' align='center' >")
		sbTL.Append(" <a  class='delbutton' style='cursor:pointer;' onclick=""RemoveRow(this)"";> </a> ")
		sbTL.Append("	</td> ")
		sbTL.Append("    <td Width='25%' class='text_list_links' align='left' >")
		sbTL.Append("    <select class='OtherListTable DropDownList'  onchange='SetOtherList(this);' id='tb{4}_div_{0}1'>")
		sbTL.Append("      {1}</select>")
		sbTL.Append("	</select>")
		sbTL.Append("     </td>")
		sbTL.Append("       <td Width='25%' class='text_list_links' align='left' > ")
		sbTL.Append("       <select class='OtherList DropDownList' onchange='SetOtherListDetail(this);' id='tb{4}_div_{0}2'>")
		sbTL.Append("     {2}</select>")
		sbTL.Append("     </td> ")
		sbTL.Append("     <td Width='40%' class='text_list_links' align='left' >")
		sbTL.Append("   <div  class='Detail' id='tb{4}_div_{0}'  >{3}</div></td>")
		sbTL.Append("</tr>")

		Dim LanguageId = Session("LanguageId").ToString()
		Dim json As New List(Of App_Code.ObjectJSON)
		Using data As New VSEntities
			'' Lay ra danh sach 3 OtherListTable

            Dim l1 = (From h In data.OtherListTables Where h.LanguageId = LanguageId And
               h.TypeId = 1 Order By h.OtherListName, h.OLCode Select h.OLCode, h.OtherListName).ToList

            Dim l2 = (From h In data.OtherListTables Where h.LanguageId = LanguageId And
                h.TypeId = 2 Order By h.OtherListName, h.OLCode Select h.OLCode, h.OtherListName).ToList

            Dim l3 = (From h In data.OtherListTables Where h.LanguageId = LanguageId And
                  h.TypeId = 3 Order By h.OtherListName, h.OLCode Select h.OLCode, h.OtherListName).ToList

			'Lay ra danh sach row render HTML 
            Dim _lstDetail = (From s In data.Section03Detail Join ot In data.OtherListTables On s.TypeOtherListTable Equals ot.OLCode
                      Join o In data.OtherLists On o.Id Equals s.IdOtherList And o.Type Equals s.TypeOtherListTable
              Where s.Section03Id = strSection03ID
              Order By ot.TypeId, ot.OtherListName, ot.OLCode
              Select s, ot.TypeId
              ).ToList

			Dim _sb1 As New StringBuilder
			Dim _sb2 As New StringBuilder
			Dim _sb3 As New StringBuilder
			For index = 0 To _lstDetail.Count - 1
				Dim randIndex = New Random().Next(100)
				Dim otlistTB = _lstDetail(index).s.TypeOtherListTable
				Dim otlist = _lstDetail(index).s.IdOtherList

				Dim _objOL = (From q In data.OtherLists
								Where q.Type.Contains(otlistTB)
							  Order By q.PhanLoai
							  Select q.Id, q.Name).ToList()

				Dim _OLTB As New StringBuilder
				Dim _OL As New StringBuilder
				Dim _otherDetail As String = GetOtherListDetail(otlistTB, otlist)

				Select Case _lstDetail(index).TypeId
					Case 1
						For Each item In l1
							_OLTB.Append("<option value='" + item.OLCode + "' " + If(item.OLCode = otlistTB, "selected='selected'", "") + ">" + item.OtherListName + "</option>")
						Next
						'
						For Each item In _objOL
							_OL.Append("<option value='" + item.Id + "' " + If(item.Id = otlist, "selected='selected'", "") + ">" + item.Name + "</option>")
						Next
						_sb1.Append(String.Format(sbTL.ToString, randIndex.ToString, _OLTB.ToString(), _OL.ToString, _otherDetail, "1"))
					Case 2
						For Each item In l2
							_OLTB.Append("<option value='" + item.OLCode + "' " + If(item.OLCode = otlistTB, "selected='selected'", "") + ">" + item.OtherListName + "</option>")
						Next
						'
						For Each item In _objOL
							_OL.Append("<option value='" + item.Id + "' " + If(item.Id = otlist, "selected='selected'", "") + ">" + item.Name + "</option>")
						Next
						_sb2.Append(String.Format(sbTL.ToString, randIndex.ToString, _OLTB.ToString(), _OL.ToString, _otherDetail, "2"))

					Case 3
						For Each item In l3
							_OLTB.Append("<option value='" + item.OLCode + "' " + If(item.OLCode = otlistTB, "selected='selected'", "") + ">" + item.OtherListName + "</option>")
						Next
						''=
						For Each item In _objOL
							_OL.Append("<option value='" + item.Id + "' " + If(item.Id = otlist, "selected='selected'", "") + ">" + item.Name + "</option>")
						Next
						_sb3.Append(String.Format(sbTL.ToString, randIndex.ToString, _OLTB.ToString(), _OL.ToString, _otherDetail, "3"))
				End Select
			Next

			Dim obj1 As New App_Code.ObjectJSON
			obj1.ID = "1"
			obj1.FullText = _sb1.ToString
			json.Add(obj1)

			Dim obj2 As New App_Code.ObjectJSON
			obj2.ID = "2"
			obj2.FullText = _sb2.ToString
			json.Add(obj2)

			Dim obj3 As New App_Code.ObjectJSON
			obj3.ID = "3"
			obj3.FullText = _sb3.ToString
			json.Add(obj3)

			Return json
		End Using
	End Function

	<WebMethod()> _
   <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
	Public Function GetAutoCompleteVendorMSDS(ByVal strSrc As String) As List(Of App_Code.ObjectJSON)
		Dim json As New List(Of App_Code.ObjectJSON)

		Using data As New VSEntities
			'Dim p = data.uspGetPhanLoaiNhomThuocChildSelectAll(strSrc, 0, 10).ToList()
            Dim p = (From q In data.MSDSOriginals
              Where (q.ItemCode.Contains(strSrc) Or q.ItemName.Contains(strSrc) Or strSrc = "") And (q.StatusId = 3 Or q.StatusId = 2)
               Group By _material = q.ItemCode Into GroupMaterial = Group
              Select New With {_material, ._fiscal = GroupMaterial.FirstOrDefault().ItemName}).ToList()

			''Export du lieu sang kieu du lieu json
			If Not p Is Nothing AndAlso p.Count > 0 Then
				For i As Integer = 0 To p.Count - 1
					Dim objTacGia As New App_Code.ObjectJSON
					objTacGia.ID = p(i)._material
					objTacGia.FullText = p(i)._fiscal
					json.Add(objTacGia)
				Next
			End If
			Return json
		End Using
	End Function

	<WebMethod()> _
   <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
	Public Function GetListVendorMSDS(ByVal itemCode As String) As List(Of App_Code.ObjectJSON)
		Dim json As New List(Of App_Code.ObjectJSON)
		'ConvertVietNamToNoneVietNam(strSrc)
		Using data As New VSEntities
            Dim p = (From q In data.MSDSOriginals
              Where q.ItemCode.Contains(itemCode) And (q.StatusId = 3 Or q.StatusId = 2)
              Select q.VendorCode, q.Vendorname).ToList()
			''Export du lieu sang kieu du lieu json
			If Not p Is Nothing AndAlso p.Count > 0 Then
				For i As Integer = 0 To p.Count - 1
					Dim objTacGia As New App_Code.ObjectJSON
					objTacGia.ID = p(i).VendorCode
					objTacGia.FullText = p(i).Vendorname
					json.Add(objTacGia)
				Next
			End If
			Return json
		End Using
	End Function

	<WebMethod()> _
	   <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
	Public Function GetDataSection02Detail(ByVal strSection02ID As String) As String
		Dim json As String = ""
		Using data As New VSEntities
			Dim p = (From q In data.Section02Detail
			  Where q.Section02Id = strSection02ID).ToList()
			''Export du lieu sang kieu du lieu json
			If Not p Is Nothing AndAlso p.Count > 0 Then
				For i As Integer = 0 To p.Count - 1
                    json &= (p(i).ThanhPhanNguyHiem & "@" & p(i).SoCAS & "@" & p(i).CongThucHoaHoc & "@" & p(i).HamLuong & "$")
				Next
			End If
			Return json
		End Using
	End Function

	<WebMethod()> _
	   <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
	Public Function GetDataSection11Detail(ByVal strSection11ID As String) As String
		Dim json As String = ""
		Using data As New VSEntities
			Dim p = (From q In data.Section11Detail
			  Where q.Section11Id = strSection11ID).ToList()
			''Export du lieu sang kieu du lieu json
			If Not p Is Nothing AndAlso p.Count > 0 Then
				For i As Integer = 0 To p.Count - 1
					json &= (p(i).TenThanhPhan & "#" & p(i).LoaiNguong & "#" & p(i).KetQua & "#" & p(i).DuongTiepXuc & "#" & p(i).SinhVatThu & "$")
				Next
			End If
			Return json
		End Using
	End Function

	<WebMethod()> _
	   <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
	   Public Function GetDataSection12Detail(ByVal strSection12ID As String) As String
		Dim json As String = ""
		Using data As New VSEntities
			Dim p = (From q In data.Section12Detail
			  Where q.Section12Id = strSection12ID).ToList()
			''Export du lieu sang kieu du lieu json
			If Not p Is Nothing AndAlso p.Count > 0 Then
				For i As Integer = 0 To p.Count - 1
					json &= (p(i).TenThanhPhan & "#" & p(i).LoaiSinhVat & "#" & p(i).ChukyAnhHuong & "#" & p(i).KetQua & "$")
				Next
			End If
			Return json
		End Using
	End Function
End Class